Toggle switch
Allow users to turn settings or options on or off with immediate effect.
#Examples
Think of ToggleSwitch as a physical switch that allows the user to turn things on or off, like a light switch. Use ToggleSwitch when the user has the freedom and control to change their settings as needed.
A ToggleSwitch should always have a label and a default value, either true (on) or false (off).
const [checked, setChecked] = React.useState(false);
return <ToggleSwitch label="Label" value={checked} onChange={setChecked} />;#Label Position
Depending on layout constraints, labels can be positioned to the left or right of a ToggleSwitch. It is best to place the labels to the left of the ToggleSwitch so that users can more easily read the text and understand the meaning in the language written from left to right.
const [checked, setChecked] = React.useState(false);
return (
<>
<ToggleSwitch
label="Label in the left"
labelPosition="left"
value={checked}
onChange={setChecked}
/>
<br />
<ToggleSwitch
label="Label in the right"
labelPosition="right"
value={checked}
onChange={setChecked}
/>
</>
);#Custom Label
Use a custom label to explain what happens when a ToggleSwitch is on or off. Explain how turning a switch on or off affects a user's decision.
const [checked, setChecked] = React.useState(false);
return (
<ToggleSwitch
label={
<InlineText emphasis={checked ? "medium" : "normal"}>
<Tooltip variant={{ type: "text" }} content="This is a tooltip">
Label with tooltip
</Tooltip>
</InlineText>
}
value={checked}
onChange={setChecked}
/>
);| Property | Description | Defined | Value |
|---|---|---|---|
onChangeRequired | functionCallback to switch the value | ||
valueRequired | booleanDetermines the state of the toggle switch | ||
labelOptional | elementText to be displayed next to the toggle switch | ||
aria-labelOptional | stringText to describe the function of toggle switch | ||
aria-describedbyOptional | stringID of an an element that describes what the toggle switch is for | ||
disabledOptional | booleanDisabled state | ||
labelPositionOptional | "left" | "right"Should the label be displayed on the right or the left side of the toggle switch - default is right | ||
fullWidthOptional | booleanShould the toggle element be full width? | ||
data-observe-keyOptional | stringUnique string, used by external script e.g. for event tracking | ||
classNameOptional | stringCustom className that's applied to the outermost element (only intended for special cases) | ||
styleOptional | objectStyle object to apply custom inline styles (only intended for special cases) | ||
tabIndexOptional | numberTab index of the outermost HTML element of the component | ||
onKeyDownOptional | functionCallback for onKeyDown event | ||
onMouseDownOptional | functionCallback for onMouseDown event | ||
onMouseEnterOptional | functionCallback for onMouseEnter event | ||
onMouseLeaveOptional | functionCallback for onMouseLeave event | ||
onFocusOptional | functionCallback for onFocus event | ||
onBlurOptional | functionCallback for onBlur event |
#Guidelines
#Best practices
#Do not use when
#Accessibility
Explore detailed guidelines for this component: Accessibility Specifications